# Author: Allen Hutchison
# Copyright (C) 2006 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#      Unless required by applicable law or agreed to in writing, software
#      distributed under the License is distributed on an "AS IS" BASIS,
#      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#      See the License for the specific language governing permissions and
#      limitations under the License.
#
# This Oaf Package is the skeleton for creating your own results
# reporting agent. You should start by subclassing this package.
# Look in the Results directory for an example.

package Oaf::Results;
use base qw(Oaf);

use strict;
use warnings;

sub new {
  my($class, @args) = @_;

  my $self = $class->SUPER::new(@args);

  bless $self, $class;

  return $self;
}

sub log_results {
  my($self) = @_;
  $self->fatal_error("sub-class Oaf::Results, and override log_results");
}

sub notify {
  my($self) = @_;
  $self->fatal_error("sub-class Oaf::Results, and override notify");
}

# summary()
#  Create a summary of the test run. This will be stored
#  in the Test::Result object. There are probably several cases where
#  you will want to override this method, but it's not required.
sub summary {
  my($self, $results) = @_;

  my $test_start = 0;
  my $test_end = 0;
  my $total_tests = 0;
  my $test_passed_count = 0;
  my $test_failed_count = 0;
  my(@test_passed, @test_failed);

  # $results is a reference to an array of Oaf::Test objects.
  foreach my $test (@{$results}) {
    $total_tests++;

    if($test_start == 0) {
      $test_start = $test->start_time();
    }
    $test_end = $test->end_time();

    if ($test->exit_code() == 0) {
      $test_passed_count++;
      push @test_passed, $test;
    } else {
      $test_failed_count++;
      push @test_failed, $test;
    }
  }
  $self->total_tests($total_tests);
  $self->start_time($test_start);
  $self->end_time($test_end);
  $self->test_passed_count($test_passed_count);
  $self->test_failed_count($test_failed_count);
  $self->tests_passed(\@test_passed);
  $self->tests_failed(\@test_failed);
  return;
}



my %passthrough = map {$_ => 1} qw/test_start test_end total_tests start_time
				   end_time test_passed_count test_failed_count
				   tests_passed tests_failed/;

sub AUTOLOAD {
  my($self, $arg) = @_;
  my $method = our $AUTOLOAD;
  $method =~ s/.*:://;
  return unless $method =~ /[^A-Z]/; # skip DESTROY and all-cap methods.
  $self->warning("invalid attribute method: ->$method( )") 
    unless $passthrough{$method};
  $self->{uc $method} = $arg if defined($arg);
  return $self->{uc $method};
}

sub duration {
  my($self) = @_;
  my $start = $self->test_start();
  my $end = $self->test_end();
  if (defined($start) and defined($end)) {
    return $end - $start;
  }
}

sub test_passed_percent {
  my($self) = @_;
  my $passed = $self->test_passed_count();
  my $total = $self->total_tests();
  if($total != 0) {
    return sprintf "%.2f", $passed / $total * 100;
  } else {
    return 0;
  }
}

sub test_failed_percent {
  my($self) = @_;
  my $failed = $self->test_failed_count();
  my $total = $self->total_tests();
  if($total != 0) {
    return sprintf "%.2f", $failed / $total * 100;
  } else {
    return 0;
  }
}

# Have to eval to something
1;
__END__
